package cn.com.dcsgo.springcloudstudy.order.controller;

import cn.com.dcsgo.springcloudstudy.order.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Dcsgo
 * @since 2024-08-06
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class OrderHystrixController {
    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    String paymentInfoOk(@PathVariable Integer id) {
        return paymentHystrixService.paymentInfoOk(id);
    }


    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable Integer id) {
        return paymentHystrixService.paymentInfoTimeout(id);
    }

    @GetMapping("/consumer/payment/hystrix/run/error/{id}")
    public String paymentInfoRunError(@PathVariable Integer id) {
        return paymentHystrixService.paymentInfoRunWithError(id);
    }

    @GetMapping("/consumer/hystrix/run/error/{id}")
    @HystrixCommand(fallbackMethod = "runErrorFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    public String runError(@PathVariable Integer id) {
        int age = 10 / 0;
        return "就不可能走到这儿";
    }

    //服务降级调用的方法
    public String runErrorFallbackMethod(Integer id) {
        return "服务端或本地服务运行错误，请联系运维人员";
    }

    /**
     * 全局fallback
     * @return string
     */
    public String paymentGlobalFallbackMethod() {
        return "服务端或本地服务运行错误，请联系运维人员(全局服务降级)";
    }
}
